package com.hefeng.wx.service.Impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hefeng.common.core.utils.StringUtils;
import com.hefeng.wx.domain.WxResidence;
import com.hefeng.wx.domain.WxUser;
import com.hefeng.wx.mapper.WxResidenceMapper;

import com.hefeng.wx.service.WxResidenceService;
import com.hefeng.wx.service.WxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;

/**
 * 小区;(wx_residence)表服务实现类
 * @author : hzh
 * @date : 2022-9-2
 */
@Service
public class WxResidenceServiceImpl extends ServiceImpl<WxResidenceMapper,WxResidence> implements WxResidenceService {
    @Autowired
    private WxResidenceMapper wxResidenceMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public WxResidence queryById(String id){
        return wxResidenceMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param wxResidence 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    public Page<WxResidence> paginQuery(WxResidence wxResidence, long current, long size){
        //1. 构建动态查询条件
        LambdaQueryWrapper<WxResidence> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(wxResidence.getName())){
            queryWrapper.eq(WxResidence::getName, wxResidence.getName());
        }
        if(StringUtils.isNotBlank(wxResidence.getCitycode())){
            queryWrapper.eq(WxResidence::getCitycode, wxResidence.getCitycode());
        }

        if(StringUtils.isNotBlank(wxResidence.getTowncode())){
            queryWrapper.eq(WxResidence::getTowncode, wxResidence.getTowncode());
        }

        //2. 执行分页查询
        Page<WxResidence> pagin = new Page<>(current , size , true);
        IPage<WxResidence> selectResult = wxResidenceMapper.selectByPage(pagin , queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }

    /**
     * 新增数据
     *
     * @param wxResidence 实例对象
     * @return 实例对象
     */
    public WxResidence insert(WxResidence wxResidence){
        wxResidenceMapper.insert(wxResidence);
        return wxResidence;
    }

    /**
     * 更新数据
     *
     * @param wxResidence 实例对象
     * @return 实例对象
     */
    public WxResidence update(WxResidence wxResidence){
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<WxResidence> chainWrapper = new LambdaUpdateChainWrapper<WxResidence>(wxResidenceMapper);
        if(StringUtils.isNotBlank(wxResidence.getName())){
            chainWrapper.eq(WxResidence::getName, wxResidence.getName());
        }
        if(StringUtils.isNotBlank(wxResidence.getCitycode())){
            chainWrapper.eq(WxResidence::getCitycode, wxResidence.getCitycode());
        }
        if(StringUtils.isNotBlank(wxResidence.getAddress())){
            chainWrapper.eq(WxResidence::getAddress, wxResidence.getAddress());
        }
        if(StringUtils.isNotBlank(wxResidence.getLongitude())){
            chainWrapper.eq(WxResidence::getLongitude, wxResidence.getLongitude());
        }
        if(StringUtils.isNotBlank(wxResidence.getLatitude())){
            chainWrapper.eq(WxResidence::getLatitude, wxResidence.getLatitude());
        }
        if(StringUtils.isNotBlank(wxResidence.getCreatedUser())){
            chainWrapper.eq(WxResidence::getCreatedUser, wxResidence.getCreatedUser());
        }
        //2. 设置主键，并更新
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if(ret){
            return queryById(wxResidence.getId().toString());
        }else{
            return wxResidence;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param undefinedId 主键
     * @return 是否成功
     */
    public boolean deleteById(String undefinedId){
        int total = wxResidenceMapper.deleteById(undefinedId);
        return total > 0;
    }
}
